import 'dart:convert' show json;
import 'package:ribo_base_core/base_styles/base_chart_style.dart';
import 'base_model_asT.dart';

class BaseChartModel {

  /// 扩展，参考flutter_echarts用法
  List<String> extensions;

  //option
  Map<String, dynamic> option;

  /// 图表样式
  BaseChartStyle style;

  /// 点击item回调
  Function(String) itemPress;

  /// 触发点击方法
  String extraScript;

  BaseChartModel({
    this.option,
    this.extensions,
    this.style,
    this.itemPress,
    this.extraScript,
  });

  factory BaseChartModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    return BaseChartModel(
      style: asT<BaseChartStyle>(jsonRes['style']),
      extensions: asT<List<String>>(jsonRes['extensions']),
      option: asT<Map<String, dynamic>>(jsonRes['option']),
      itemPress: asT<Function(String)>(jsonRes['itemPress']),
      extraScript: asT<String>(jsonRes['extraScript']),
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'extensions': extensions,
        'style': style,
        'option': option,
        'itemPress': itemPress,
        'extraScript': extraScript,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
